package cn.ryh.gatewaycode.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author Rao
 * @Description 自定义全局GlobalFilter 需实现 GlobalFilter, Ordered接口
 * @create 2021-12-08 13:54
 */
@Component
public class GatewayLogFilterTwo implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("**************GatewayLogFilterTwo pre:" + 1);
        String username = exchange.getRequest().getQueryParams().getFirst("username");
        return chain.filter(exchange).then(Mono.fromRunnable(()->{
            System.out.println("**************GatewayLogFilterTwo post:" + 1);
        }));
    }

    /**
     * 表示其加载的顺序
     * 越小优先级越高。
     * @return
     */
    @Override
    public int getOrder() {
        return 1;
    }
}
